Shader "Custom/BranchTextured" 
{
    Properties 
	{
        _MainTex ("Base (RGB)", 2D) = "white" {}
		_DisplacementTex ("Displacement", 2D) = "white" {}
		_SineFrequency ("Sine Frequency", Float) = 3.0
		_SineAmplitude ("Sine Amplitude", Float) = 4.0
		_AlphaCutoff ("Alpha Cutoff", Float) = 0.8
	}
	SubShader 
	{
        Tags {"LightMode" = "Vertex" "Queue"="Transparent" "RenderType"="Transparent"}
		LOD 100
		
		Alphatest Greater 0
		Blend SrcAlpha OneMinusSrcAlpha

		CGPROGRAM
		#pragma target 3.0
		#pragma glsl
		#pragma surface surf Lambert
		
		sampler2D _MainTex;
		sampler2D _DisplacementTex;
		fixed _SineFrequency;
		fixed _SineAmplitude;
		fixed _AlphaCutoff;

		// Random number used by 2D noise function...
		float random(float2 ab) 
		{
			float f = (cos(dot(ab ,float2(21.9898,78.233))) * 43758.5453);
			return frac(f);
		}
		// A 2D noise function...
		float snoise(in float2 xy) 
		{
			float2 ij = floor(xy);
			float2 uv = xy-ij;
			uv = uv*uv*(3.0-2.0*uv);
	
			float a = random(float2(ij.x, ij.y ));
			float b = random(float2(ij.x+1., ij.y));
			float c = random(float2(ij.x, ij.y+1.));
			float d = random(float2(ij.x+1., ij.y+1.));
			float k0 = a;
			float k1 = b-a;
			float k2 = c-a;
			float k3 = a-b-c+d;
			return (k0 + k1*uv.x + k2*uv.y + k3*uv.x*uv.y);
		}

        struct Input 
		{
            float2 uv_MainTex;
			float2 uv_DisplacementTex;
        };

        void surf (Input IN, inout SurfaceOutput o) 
		{
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;

			half d = tex2D (_DisplacementTex, IN.uv_DisplacementTex);
			float alpha = sin(IN.uv_MainTex.y * _SineFrequency) * _SineAmplitude * d;
			if(alpha < _AlphaCutoff)
				alpha = 0.0;
            o.Alpha = alpha;
        }

		ENDCG
	} 
	FallBack "Diffuse"
}
